/**
 * 
 */
package com.mc.date;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

/**
 * 字符串时间处理
 * @author w.weizhen
 *
 */
public class StringDateUtil {
    /**
     * @param args
     */
    public static void main(String[] args) {
        
//        String date = "2022/4/25 8:45",format="yyyy/M/dd H:mm";
//        LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format));
        
//        Date date = new Date();
//        String fromFormat = "yyyy-MM-dd";
//        SimpleDateFormat sdf = new SimpleDateFormat(fromFormat);
//        String foStrDate = sdf.format(date);
//        System.out.println(foStrDate);
        
        //int calendarNum = Calendar.MONTH;
//        int calendarNum = Calendar.YEAR;
//        String toStrDate = strDateCalculation(fromFormat, foStrDate, calendarNum, 50);
//        System.out.println(toStrDate);
        
        //format();
        
        //dataTolong();
        
        //longTodata();
        
        //listMiddleDate("2021-07-21", "2021-07-21");
        
//        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        LocalDate ldt = LocalDate.parse("2021-07-22", dtf);
//        LocalDate twoDaysLater = ldt.plusDays(-6);
//        System.out.println(twoDaysLater.format(dtf));
        
//        String day = "2021-07-11";
//        //今天的日期
//        String today = "2021-07-21";
//        if(StringUtils.isBlank(day))
//        {
//            day = today;
//        }
//        else
//        {
//            // 获取两个日期之间的所有日期,如果所有日期的数量大于7,则获取今天之前的第7天的日期.
//            List<String> days = listMiddleDate(day, today);
//            if(days.size()>7)
//            {
//                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//                LocalDate ldt = LocalDate.parse(today, dtf);
//                LocalDate twoDaysLater = ldt.plusDays(-6);
//                day = twoDaysLater.format(dtf);
//            }
//        }
//        //将日期字符串,转为时间戳[毫秒]
//        day = day + " 00:00:00";
//        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        LocalDateTime ldt = LocalDateTime.parse(day, dtf);
//        Long epochMilli = LocalDateTime.from(ldt).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
//        String date = epochMilli.toString();
//        System.out.println(date);
        
        
//        System.currentTimeMillis();
//        System.out.println(System.currentTimeMillis());
        
//        System.out.println(System.nanoTime());
        
//        System.out.println(System.currentTimeMillis());
        
//        System.out.println(System.currentTimeMillis()/1000/(60*60*24));
//        
//        System.out.println(1000*(60*60*24));
        
//        String format = DateUtil.format(DateUtil.offset(DateUtil.date(), DateField.MINUTE, -5), "yyyy-MM-dd'T'HH:mm:ss");
//        System.out.println(format);
        
//        String last = "20221229135100";
//        String time = "20221229135102";
////    	long between = DateUtil.between(DateUtil.parse(last), DateUtil.parse(time), DateUnit.MS);
////        System.out.println(between);
//        
//        long xxx = Long.parseLong(last) - Long.parseLong(time);
//        System.out.println(xxx);
        
    	
    	//System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    	
        
        
    }
    
    /**
     * 增加或者减少传入日期的内容
     * <一句话功能简述>  <br>
    * <功能详细描述> 
    * @param fromFormat 日期时间格式
    * @param strDate 被改变的日期时间
    * @param calendarNum 被改变的数据类型[Calendar.MINUTE分钟,Calendar.DAY_OF_YEAR 天(这两个较为普遍) ]
    * @param num
    * @return 
    * @see [类、类#方法、类#成员]
     */
    public static String strDateCalculation(String fromFormat, String strDate, int calendarNum, int num)
    {
        try
        {
            SimpleDateFormat sdf=new SimpleDateFormat(fromFormat);
            Calendar nowTime1 = Calendar.getInstance();
            Date date1 = sdf.parse(strDate);
            nowTime1.setTime(date1);
//            nowTime1.add(Calendar.MINUTE, 30);//要改变什么，改变多少.
            nowTime1.add(calendarNum, num);
            strDate = sdf.format(nowTime1.getTime());
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return strDate;
        }
        return strDate;
    }
    
    /**
     * 将字符串转日期成Long类型的时间戳
     */
    public static void format() { 
        String date="2021-07-21 00:00:00"; 
        String from="yyyy-MM-dd HH:mm:ss";
        String to="yyyy-MM-dd";
        LocalDateTime time = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(from));
        String x = time.format(DateTimeFormatter.ofPattern(to));
        System.out.println(x);
    }
    
    public static void longTodata()
    {
        String forma = "yyyy-MM-dd HH:mm:ss";
        Long milli = Long.parseLong("1626923812000");
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern(forma);
        String date = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(milli),ZoneId.systemDefault()));
        System.out.println(date);
    }
    
    public static void dataTolong()
    {
        String forma = "yyyy-MM-dd HH:mm:ss";
        String date = "2021-07-22 11:16:52";
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern(forma);
        LocalDateTime parse = LocalDateTime.parse(date, ftf);
        Long milli = LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        System.out.println(milli);
    }
    
    // 两个日期之间的所有日期集合
    public static List<String> listMiddleDate(String begin, String end)
    {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate beginLdt = LocalDate.parse(begin, dtf);
        LocalDate endLdt = LocalDate.parse(end, dtf);
        List<String> datas = new ArrayList<String>();
        long length = endLdt.toEpochDay() - beginLdt.toEpochDay();
        for (long i = length; i >= 0; i--) {
            datas.add(endLdt.minusDays(i).format(dtf));
        }
        System.out.println(datas.toString());
        return datas;
    }
}